<template>
  <div class="home">
    <doudianHomePage />
    <dianshanluopan />
    <button @click="getInitTopData">查看数据</button>
  </div>
</template>

<script lang="ts" setup>
import doudianHomePage from './components/doudianHomePage/index.vue';
import dianshanluopan from './components/dianshanluopan/index.vue';

import { ref, provide } from 'vue';
import { initTopDataCf, initJinyingDataCf, initTiyangfenDataCf, initDslpDataCfFun } from '../dataConfig/initPageData.ts'

// 顶部 代发货数据

// 设置type
interface TopData {
  title: string,
  key: string,
  listIds: {
    title: string,
    key: string,
    val: number,
    status: Boolean
  }[]
}
interface initJinyingDataType {
  title: string,
  key: string,
  val: number,
  val_status: boolean,
  sub_val: number,
  val_show_tab: string | number,
  sub_val_status: boolean,
  icon_type: string,
  icon_type_status: boolean
}
function savaDataInStorege(key: String, data: TopData[] | initJinyingDataType[] | any) {
  console.log(key, data)

  chrome.storage.local.set({ [key as string]: JSON.parse(JSON.stringify(data)) }, function () {
    console.log('Value is set to ' + data);
  });
}

function getInitTopData() {
  chrome.storage.local.get(['initTopData', 'initJinyingData', 'initTiyangfenData', 'initDslpData'], function (result) {
    // console.log('result.initTiyangfenData', result.initTiyangfenData);
    if (result.initTopData) {
      initTopData.value = result.initTopData
    } else {
      savaDataInStorege('initTopData', initTopData.value)
    }
    if (result.initJinyingData) {
      initJinyingData.value = result.initJinyingData
    } else {
      savaDataInStorege('initJinyingData', initJinyingData.value)
    }
    if (result.initTiyangfenData) {
      console.log('result.initTiyangfenData', result.initTiyangfenData);
      initTiyangfenData.value = result.initTiyangfenData
    } else {
      savaDataInStorege('initTiyangfenData', initTiyangfenData.value)
    }
    if (result.initDslpData) {
      console.log('result.initDslpData', result.initDslpData)
      initDslpData.value = result.initDslpData
    } else {
      savaDataInStorege('initDslpData', initDslpData.value)
    }
  });

  // chrome.storage.local.get(['initJinyingData'], function (result) {
  //   console.log(result.initJinyingData);


  // });
}
// 初始化数据
function init() {
  getInitTopData()
}
init()

// 顶部待发货等数据
const initTopData = ref<TopData[]>(initTopDataCf);
provide('initTopData', { initTopData, savaDataInStorege })

// 经营数据
const initJinyingData = ref<initJinyingDataType[]>(initJinyingDataCf)
provide('initJinyingData', { initJinyingData, savaDataInStorege })

// 体验分等数据
const initTiyangfenData = ref<any>(initTiyangfenDataCf)
provide('initTiyangfenData', { initTiyangfenData, savaDataInStorege })


// 电商罗盘 展示数据
const initDslpData = ref<any>(initDslpDataCfFun())
provide('initDslpData', { initDslpData, savaDataInStorege })


</script>